# ~*~ coding: utf-8 ~*~
from flask_wtf import FlaskForm
from wtforms import StringField
from wtforms.validators import InputRequired

from apps.common.form import ErrorFormMinx

__all__ = ['AuthCreateUpdateForm']


class AuthCreateUpdateForm(ErrorFormMinx, FlaskForm):
    name = StringField(validators=[InputRequired(message="权限名称不能为空")])
    user_ids = StringField(validators=[InputRequired(message="请选择授权的用户")])
    system_user_ids = StringField(validators=[InputRequired(message="系统用户不能为空")])
    asset_ids = StringField(validators=[InputRequired(message="资产不能为空")])
    actions = StringField()
    is_active = StringField()
    comment = StringField()

    class Meta:
        csrf = True

    @property
    def data(self):
        return {
            'name': self.name.data,
            'user_ids': self.user_ids.data,
            'system_user_ids': self.system_user_ids.data,
            'asset_ids': self.asset_ids.data,
            'actions': self.actions.data,
            'is_active': int(self.is_active.data) if self.is_active.data else 0,
            'comment': self.comment.data
        }
